<template>
  <div class="pcMenu">
    <QueryForm
      ref="conditionForm"
      module="pcMenu"
      :searchBtnLoading="loading"
      v-model:searchForm="searchForm"
      v-model:search-column="searchColumn"
      @handle-search="onSearch"
    />
    <div class="common-box">
      <el-row style="height: 40px" class="con-button-rows">
        <ButtonGroup
          :disabledDelete="multipleSelection.length != 1"
          :disabledEdit="multipleSelection.length != 1"
          :hasPermiAdd="['system:roles:add']"
          :hasPermiDelete="['system:roles:delete']"
          :hasPermiEdit="['system:roles:edit']"
          @add="onAdd"
          @edit="onEdit"
          @delete="onDelete"
        >
          <el-button
            icon="Lock"
            v-hasPermi="['system:roles:lock']"
            :disabled="multipleSelection.length != 1"
            @click="onLock"
          />
        </ButtonGroup>
      </el-row>
      <MyTable
        tableId="pcRoles"
        :columns="columns"
        :dataSource="dataSource"
        :loading="loading"
        :checkStrictly="true"
        :pageVo="pageVo"
        ref="commonTable"
        moduleName="pcRoles"
        @handleSelectionChange="handleSelectionChange"
        @handle-page-change="handlePageChange"
      ></MyTable>
    </div>
    <Add
      v-if="dialogVisible"
      v-model:visible="dialogVisible"
      :currRow="currRow"
      :isType="isType"
      @onSubmit="onSearch"
    ></Add>
    <!-- 权限设置弹框 -->
    <PermissionRoles
      v-if="permissionVisible"
      :params="dialogParams"
      v-model:permission-visible="permissionVisible"
      @role-func-submit="handleRoleFuncSubmit"
    />
  </div>
</template>

<script setup lang='tsx'>
import { ref, reactive, onMounted, onUnmounted } from "vue";
import { useI18n } from "vue-i18n";
import i18n from "@/lang/index";
import Add from "./components/Add.vue";
import * as api from "@/api/index.js";
import { ElMessage, ElMessageBox } from "element-plus";
import { hasPermi } from "@/utils/mUtils.js";
import { queryFormColumnFilters, getSubZhEn } from "@/utils/mUtils.js";
import PermissionRoles from "./components/permission-roles.vue";
let { t } = useI18n();
const dataRoles = [
  { label: "全部数据权限", value: "1" },
  { label: "自定数据权限", value: "2" },
  { label: "当前组织数据权限", value: "3" },
  { label: "当前组织及下级数据权限", value: "4" },
  { label: "当前用户数据权限", value: "5" },
];

defineOptions({
  name: "PcRoles",
});
const pageVo = reactive({
  pageSize: 50,
  total: 10,
  currentPage: 1,
  status: true,
});
const loading = ref(true);
const dataSource = ref([]);
const columns = ref([
  {
    type: "checkbox",
    width: 50,
    align: "center",
    fixed: "left",
  },
  {
    title: "序号",
    type: "seq",
    width: 60,
    fixed: "left",
  },
  {
    field: "roleSort",
    title: "排序编码",
  },
  {
    field: "roleName",
    title: "角色名称",
  },
  {
    field: "dataScope",
    title: "数据权限",
    formatter: ({ cellValue }) => {
      const row = dataRoles.find((it) => it.value === cellValue);
      return row ? t(row.label) : "";
    },
  },
  {
    field: "siteRankName",
    title: "机构级别",
  },
  {
    field: "status",
    title: "状态",
     filters: [
      { label: i18n.global.t("启用"), value: "0" },
      { label: i18n.global.t("禁用"), value: "1" },
    ],
    slots: { default: "status_default" },
    params: {
      color: {
        1: "gray",
        0: "green",
      },
    },
  },
]);
const searchForm = reactive({});
const searchColumn = ref([
  {
    type: "label_input",
    label: "角色名称",
    prop: "roleName",
  },
  {
    type: "label_select",
    label: "机构级别",
    options: [],
    prop: "siteRankCode",
  },
]);
const dialogVisible = ref(false);
const multipleSelection = ref([]);
const currRow = ref({});
const isType = ref("add");
const permissionVisible = ref(false);
const dialogParams = ref({});
onMounted(() => {
  onSearch();
  onGetSub();
});
const onGetSub = () => {
  getSubZhEn({
    code: "001",
  }).then((res) => {
    queryFormColumnFilters(searchColumn.value, "siteRankCode").options =
      res["001"];
  });
};
const roles = () => {
  loading.value = true;
  api.default.system.role
    .list({
      ...searchForm,
      pageNum: pageVo.currentPage,
      pageSize: pageVo.pageSize,
    })
    .then((res) => {
      dataSource.value = res.rows;
      loading.value = false;
      pageVo.total = res.total;
    })
    .catch(() => {
      loading.value = false;
    });
};

const onAdd = () => {
  isType.value = "add";
  if (multipleSelection.value.length != 0) {
    currRow.value = multipleSelection.value[0];
  }
  dialogVisible.value = true;
};
const onEdit = () => {
  isType.value = "edit";
  currRow.value = multipleSelection.value[0];
  dialogVisible.value = true;
};
const onChangeMenuType = (row) => {
  api.default.system.menu.edit({ ...row }).then(() => {
    onSearch();
  });
};
const onDelete = () => {
  const data = multipleSelection.value[0];
  ElMessageBox.confirm(t("是否确定删除当前选择数据？"), t("提示"), {
    confirmButtonText: t("确定"),
    cancelButtonText: t("取消"),
    type: "warning",
  })
    .then(() => {
      api.default.system.menu.remove({ menuId: data.menuId }).then(() => {
        ElMessage.success(t("删除成功"));
        onSearch();
      });
    })
    .catch(() => {});
};
const onLock = () => {
  const data = multipleSelection.value[0];
  Object.assign(dialogParams.value, {
    roleId: data.roleId,
    type: 1,
  });
  permissionVisible.value = true;
};
const handleRoleFuncSubmit = () => {
  permissionVisible.value = false;
};
const handleSelectionChange = (list) => {
  multipleSelection.value = list;
};
const handlePageChange = (val) => {
  const { currentPage, pageSize } = val;
  pageVo.currentPage = currentPage;
  pageVo.pageSize = pageSize;
  roles();
};
const onSearch = () => {
  pageVo.currentPage = 1;
  roles();
};
</script>

<style lang="scss" scoped>
.pcMenu {
  height: 100%;
  width: 100%;
  background: #fff;
}
</style>